package cocom.iplatform.piaoerbai.controller;

import cocom.iplatform.piaoerbai.bean.MenuBean;
import cocom.iplatform.piaoerbai.bean.ResultBean;
import cocom.iplatform.piaoerbai.constants.BaseConstants;
import cocom.iplatform.piaoerbai.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @className: MenuController
 * @description: TODO 类描述
 * @author: chensk
 * @date: 2020/12/3
 **/
@RestController
@RequestMapping("menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    @RequestMapping("selectAllMenu")
    public ResultBean selectAllMenu() {
        List<MenuBean> menuBeans=menuService.selectAllMenu();
        return new ResultBean(BaseConstants.SUCCESS,"调用成功",menuBeans);
    }

    @RequestMapping("selectChildByParentId")
    public ResultBean selectChildByParentId(Integer parentId) {
        if(parentId==null){
            return new ResultBean(BaseConstants.FAILED,"调用失败，parentId不能为null");
        }
        List<MenuBean> menuBeans=menuService.selectChildByParentId(parentId);
        return new ResultBean(BaseConstants.SUCCESS,"调用成功",menuBeans);
    }
    @RequestMapping("insertOrUpdate")
    public ResultBean insertOrUpdate(@RequestBody  MenuBean menuBean){
        if(menuBean.getId()==null&&menuBean.getParentId()==null){
            return new ResultBean(BaseConstants.FAILED,"新增的时候，parentId不能为空");
        }
        menuService.insertOrUpdate(menuBean);
        return new ResultBean(BaseConstants.SUCCESS,"调用成功");
    }

    @RequestMapping("delete")
    public  ResultBean delete(Integer id){
        menuService.deleteById(id);
        return new ResultBean(BaseConstants.SUCCESS,"删除成功");
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultBean exceptionHandler(Exception e) {
        return new ResultBean(BaseConstants.FAILED, e.getMessage());
    }

}
